/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import BackEnd.Exceptions.*;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.SQLException;

/**
 *
 * @author Ivan
 */
public class PostXCategoriaDao extends DaoBase{

    @Override
    public ValueObjectBase create(ValueObjectBase value) throws CreateRegisterException {
        if(value.getClass()==PostXCategoriaValueObject.class)
        {
            PostXCategoriaValueObject vo=(PostXCategoriaValueObject)value;
            try {
            Connection con=super.getConection("dlc");
            String sentencia="call pa_registrarPostXCategoria(?,?)";
            java.sql.CallableStatement wcall=con.prepareCall(sentencia);
            wcall.setInt(1, vo.getIdPost());
            wcall.setInt(2, vo.getIdCategoria());
            wcall.execute();
            wcall.close();
            con.close();
        } catch (SQLException ex) {
            throw new CreateRegisterException();

        }
             return vo;
        }
        else
            throw new CreateRegisterException();
    }
    

    @Override
    public void update(ValueObjectBase value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ValueObjectBase retrive(PkBase pk) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(PkBase pk) throws DeleteRegisterException {
        if(pk.getClass()==PkPostXCategoria.class)
        {
            PkPostXCategoria id=(PkPostXCategoria)pk;
            Connection con;
            try {
                con = super.getConection("dlc");
            String sentencia="call pa_borrarPostXCategoria(?,?)";
            java.sql.CallableStatement wcall=con.prepareCall(sentencia);
            wcall.setInt(1, id.getIdPost());
            wcall.setInt(2, id.getIdCategoria());
            wcall.execute();
            wcall.close();
            con.close();
             } catch (SQLException ex) {
                throw new DeleteRegisterException();
            }
        }
        else
            throw new DeleteRegisterException();
    }

    @Override
    public ValueObjectBase[] listarTodos() throws NotExistRegisterException {
        return this.mostrarConsulta("select * from postxcategoria");
    }

    @Override
    public ValueObjectBase[] mostrarConsulta(String consulta) throws NotExistRegisterException {
        PostXCategoriaValueObject[] vec=null;
            try {
                Connection con = super.getConection("dlc");
                Statement st=(Statement)con.createStatement();
                st.execute(consulta);
                ResultSet res=(ResultSet) st.getResultSet();
                res.last();
                vec = new PostXCategoriaValueObject[res.getRow()];
                res.first();
                vec[0]=this.leerVo(res);
                while(res.next())
                {
                    vec[res.getRow()-1]=this.leerVo(res);
                }
                res.close();
                con.close();

            } catch (SQLException ex) {
                throw new NotExistRegisterException();
            }
        return vec;
    }

    public ValueObjectBase[] traerCategorias(int IdPost) throws NotExistRegisterException
    {
        return this.mostrarConsulta("select * from postxcategoria where idPost="+IdPost);
    }


    @Override
    public ValueObjectBase traer(PkBase pk) throws NotExistRegisterException {
        if(pk.getClass() == PkPostXCategoria.class)
        {
            PkPostXCategoria id=(PkPostXCategoria)pk;
            PostXCategoriaValueObject vo = new PostXCategoriaValueObject();
            try {
                Connection con = super.getConection("dlc");
                String sentencia = "select * from postxcategoria where idPost="+id.getIdPost()+" and idCategoria="+id.getIdCategoria();
                Statement st=(Statement)con.createStatement();
                st.execute(sentencia);
                ResultSet res=(ResultSet) st.getResultSet();
                res.next();
                vo=this.leerVo(res);
                res.close();
                con.close();

            } catch (SQLException ex) {
                throw new NotExistRegisterException();
            }

            return vo;
        }
        else
            return null;
    }
    private PostXCategoriaValueObject leerVo(ResultSet res) throws SQLException
    {
        PostXCategoriaValueObject    vo=new PostXCategoriaValueObject();
        vo.setIdPost(res.getInt("idPost"));
        vo.setIdCategoria(res.getInt("idCategoria"));
        return vo;
    }

}
